<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>教师界面 - 查看题目申请</title>
    <style>
        /* 整体页面布局和背景设置 */
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
            background-color: #f4f4f4;
            display: flex;
            min-height: 100vh;
        }

        /* 左侧导航栏样式 */
        .sidebar {
            background-color: #1d2121;
            width: 200px;
            padding: 20px;
            color: white;
        }

        .sidebar h3 {
            margin-top: 0;
            text-align: center;
        }

        .sidebar button {
            display: block;
            width: 100%;
            margin-bottom: 10px;
            padding: 12px 25px;
            background-color: #1d2121;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 16px;
            transition: background-color 0.3s ease;
        }

        .sidebar button:hover {
            background-color: #0056b3;
        }

        /* 主要内容区域样式 */
        .main-content {
            flex: 1;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: flex-start;
            padding: 20px;
        }

        /* 标题样式 */
        h2 {
            text-align: start;
            color: #333;
            margin-bottom: 20px;
            font-size: 40px;
        }

        /* 题目申请信息表格样式 */
        #topicAppsTable {
            width: 100%;
            max-width: 800px;
            border-collapse: collapse;
            margin-bottom: 20px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
        }

        #topicAppsTable caption {
            caption-side: top;
            font-size: 24px;
            font-weight: bold;
            margin-bottom: 10px;
            color: #333;
        }

        #topicAppsTable th,
        #topicAppsTable td {
            border: 1px solid #dddddd;
            text-align: left;
            padding: 8px;
        }

        #topicAppsTable th {
            background-color: #f2f2f2;
        }

        /* 查找功能样式 */
        .search-container {
            margin-bottom: 20px;
            display: flex;
            align-items: center;
        }

        .search-container input {
            padding: 8px;
            border: 1px solid #ccc;
            border-radius: 4px;
            margin-right: 10px;
        }

        .search-container button {
            padding: 8px 16px;
            background-color: #007BFF;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }
    </style>
</head>

<body>

<div class="sidebar">
    <h3>功能导航</h3>
    <button onclick="goToIndexPage()">首页</button>
    <button id="checkStudentAppsButton">审核学生申请</button>
    <button id="submitTopicButton">提交选题</button>
    <button id="viewStudentAppsButton">查看学生申请</button>
    <button id="viewTopicAppsButton">查看题目申请</button>
</div>
<div class="main-content">
    <h2>查看题目申请</h2>

    <!-- 查找功能区域 -->
    <div class="search-container">
        <input type="text" id="searchInput" placeholder="输入教师姓名">
        <button id="searchButton" onclick="searchRecords()">查找</button>
    </div>

    <table id="topicAppsTable">
        <caption>题目申请情况</caption>
        <thead>
        <tr>
            <th>课题名</th>
            <th>课题号</th>
            <th>选题方式</th>
            <th>教师姓名</th>
            <th>教师联系方式</th>
            <th>课题要求</th>
            <th>课题状态</th>
        </tr>
        </thead>
        <tbody>
        <!-- 这里将通过JavaScript动态填充表格数据 -->
        </tbody>
    </table>
</div>

<script>
    document.addEventListener('DOMContentLoaded', function () {
        const tableBody = document.getElementById('topicAppsTable').getElementsByTagName('tbody')[0];

        function loadTopicApps() {
            // 使用fetch API从Servlet获取数据并填充表格
            fetch('GetTopicAppsServlet')
                .then(response => response.json())
                .then(data => {
                    data.forEach(topic => {
                        const row = document.createElement('tr');
                        row.innerHTML = `
                                <td>${topic.course_name}</td>
                                <td>${topic.course_id}</td>
                                <td>${topic.selection_method}</td>
                                <td>${topic.teacher_name}</td>
                                <td>${topic.teacher_contact}</td>
                                <td>${topic.course_requirements}</td>
                                <td>${topic.course_status}</td>
                            `;
                        tableBody.appendChild(row);
                    });
                })
                .catch(error => console.error('获取题目申请信息失败：', error));
        }

        function searchRecords() {
            const searchInput = document.getElementById('searchInput');
            const teacherName = searchInput.value;

            // 清空表格内容
            while (tableBody.firstChild) {
                tableBody.removeChild(tableBody.firstChild);
            }

            // 使用fetch API从Servlet根据教师姓名获取数据并填充表格
            fetch('GetTopicAppsByTeacherNameServlet?teacherName=' + teacherName)
                .then(response => response.json())
                .then(data => {
                    if (data.length === 0) {
                        const row = document.createElement('tr');
                        row.innerHTML = '<td colspan="7">未找到相关记录</td>';
                        tableBody.appendChild(row);
                    } else {
                        data.forEach(topic => {
                            const row = document.createElement('tr');
                            row.innerHTML = `
                                    <td>${topic.course_name}</td>
                                    <td>${topic.course_id}</td>
                                    <td>${topic.selection_method}</td>
                                    <td>${topic.teacher_name}</td>
                                    <td>${topic.teacher_contact}</td>
                                    <td>${topic.course_requirements}</td>
                                    <td>${topic.course_status}</td>
                                `;
                            tableBody.appendChild(row);
                        });
                    }
                })
                .catch(error => console.error('根据教师姓名获取题目申请信息失败：', error));
        }

        // 页面加载完成后先加载所有题目申请信息
        loadTopicApps();

        // 获取按钮元素并绑定点击事件
        const searchButton = document.getElementById('searchButton');
        searchButton.addEventListener('click', searchRecords);
    });
</script>
</body>

</html>